<?php
namespace app\ask\controller;

use think\Controller;

class Attention extends Controller
{
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->PostModel=model("Post.Post");
        $this->CateModel=model("Post.Category");
        $this->BusinessModel=model("Business.Business");
        $this->CommentModel=model("Post.Comment");
        $this->AttentionModel=model("Post.Attention");

        $busid=$this->request->param('busid',0,'trim');
        $this->business=$this->BusinessModel->find($busid);
        if(!$this->business)
        {
            $this->error('请先登录');
            exit;
        }
    }
    public function index()
    {
        if($this->request->isPost())
        {
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page','','trim');
            $limit=5;
            $start=($page-1)*$limit;
            $where=[
                'busid'=>$this->business->id
            ];
            if(!empty($keywords))
            {
                $where['nickname']=['LIKE',"%$keywords%"];
            }
            $list=$this->AttentionModel->with(['attent'])->where($where)->limit($start,$limit)->select();
            if(!$list)
            {
                $this->error('暂无更多数据');
                exit;
            }else
            {
                $this->success('返回关注列表',null,$list);
            }
            
        }
    }
    public function fans()
    {
        if($this->request->isPost())
        {
            $keywords=$this->request->param('keywords','','trim');
            $page=$this->request->param('page','','trim');
            $limit=5;
            $start=($page-1)*$limit;
            $where=[
                'attentid'=>$this->business->id
            ];
            if(!empty($keywords))
            {
                $where['nickname']=['LIKE',"%$keywords%"];
            }
            // halt($where);
            $list=$this->AttentionModel->with(['business'])->where($where)->limit($start,$limit)->select();
            if(!$list)
            {
                $this->error('暂无更多数据');
                exit;
            }else
            {
                $this->success('返回粉丝列表',null,$list);
            }
            
        }
    }
    // 检查关注状态
    public function check()
    {
        if($this->request->isPost())
        {
            $attentid=$this->request->param('attentid',0,'trim');
            $attention=$this->BusinessModel->find($attentid);
            if(!$attention)
            {
                $this->error('用户不存在');
                exit;
            }
            // 组装数据
            $where=[
                'busid'=>$this->business->id,
                'attentid'=>$attentid,
            ];
            $attent=$this->AttentionModel->where($where)->find();
            if(!$attent)
            {
               $this->error('暂未找到关注记录');
               exit; 
            }
            else
            {
                $this->success('有关注记录');
                exit;
            }
        }
    }
    

    // 关注
    public function add()
    {
        if($this->request->isPost())
        {
            $attentid=$this->request->param('attentid',0,'trim');
            $attention=$this->BusinessModel->find($attentid);
            if(!$attention)
            {
                $this->error('用户不存在');
                exit;
            }
            // 组装数据
            $data=[
                'busid'=>$this->business->id,
                'attentid'=>$attentid,
            ];
            $result=$this->AttentionModel->validate('common/Post/Attention')->save($data);
            if($result===FALSE)
            {
                $this->error($this->AttentionModel->getError());
                exit;
            }else
            {
                $this->success('关注成功');
                exit;
            }
        }
    }

    // 取消关注
    public function del()
    {
        if($this->request->isPost())
        {
            $attentid=$this->request->param('attentid',0,'trim');
            $action=$this->request->param('action','','trim');
            $attention=$this->BusinessModel->find($attentid);
            if(!$attention)
            {
                $this->error('用户不存在');
                exit;
            }
            if($action=='0')
            {
                // 组装数据
                $where=[
                    'busid'=>$this->business->id,//14
                    'attentid'=>$attentid,//25
                ];
                // halt($where);

                // 先找出关注记录
                $attent=$this->AttentionModel->get($where);
                
                if(!$attent)
                {
                    $this->error('暂未找到关注记录');
                    exit;
                }

                // 直接删除查询到的关注记录
                $result=$attent->delete();

                if($result===FALSE)
                {
                    $this->error('取消关注失败');
                    exit;
                }else
                {
                    $this->success('取消关注成功');
                    exit;
                }
            }
            if($action=='1')
            {
                $where=[
                    'busid'=>$attentid,//25
                    'attentid'=>$this->business->id,//14
                ];
                // halt($where);

                // 先找出关注记录
                $attent=$this->AttentionModel->get($where);
                
                if(!$attent)
                {
                    $this->error('暂未找到关注记录');
                    exit;
                }

                // 直接删除查询到的关注记录
                $result=$attent->delete();

                if($result===FALSE)
                {
                    $this->error('删除粉丝失败');
                    exit;
                }else
                {
                    $this->success('删除粉丝成功');
                    exit;
                }
            }
            
        }
    }


   
}